package com.raft.core.rpc.message;

import java.io.Serializable;
import java.util.Set;

import com.raft.core.node.NodeEndpoint;
import com.raft.core.node.NodeId;

/**用于日志快照的传输
 * @author zhangsulei
 *
 */
public class InstallSnapshotRpc implements Serializable {

    /**
	 *
	 */
	private static final long serialVersionUID = -4868638840868075788L;
	private int term; // 选举term
    private NodeId leaderId; // leader节点id
    private int lastIndex; // 日志快照中最后一条日志的索引
    private int lastTerm; // 日志快照中最后一条日志term
    private Set<NodeEndpoint> lastConfig;
    // InstallSnapshot被设计为允许交互多次的消息。考虑到日志快照可能比较大，一次传输所有数据不太现实，InstallSnapshot消息中增加了数据偏移offset和判断是否已经到最后
    // 的done字段。具体传输多少数据为好，一般根据网络环境、数据大小来决定，实现时建议做成可以配置的参数
    private int offset; // 数据偏移
    private byte[] data; // 数据
    private boolean done; // 是否是最后一条消息

    public int getTerm() {
        return term;
    }

    public void setTerm(int term) {
        this.term = term;
    }

    public NodeId getLeaderId() {
        return leaderId;
    }

    public void setLeaderId(NodeId leaderId) {
        this.leaderId = leaderId;
    }

    public int getLastIndex() {
        return lastIndex;
    }

    public void setLastIndex(int lastIndex) {
        this.lastIndex = lastIndex;
    }

    public int getLastTerm() {
        return lastTerm;
    }

    public void setLastTerm(int lastTerm) {
        this.lastTerm = lastTerm;
    }

    public Set<NodeEndpoint> getLastConfig() {
        return lastConfig;
    }

    public void setLastConfig(Set<NodeEndpoint> lastConfig) {
        this.lastConfig = lastConfig;
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public byte[] getData() {
        return data;
    }

    public int getDataLength() {
        return this.data.length;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public boolean isDone() {
        return done;
    }

    public void setDone(boolean done) {
        this.done = done;
    }

    @Override
    public String toString() {
        return "InstallSnapshotRpc{" +
                "data.size=" + (data != null ? data.length : 0) +
                ", done=" + done +
                ", lastIndex=" + lastIndex +
                ", lastTerm=" + lastTerm +
                ", leaderId=" + leaderId +
                ", offset=" + offset +
                ", term=" + term +
                '}';
    }

}
